#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################

# 转换 Makefile.inc 文件为 CMake 可识别的文件
# 并获取 'LIBTESTPROGS', '*_SOURCES', 'TESTUTIL', 'TSTTRACE', 'WARNLESS', 'MULTIBYTE', 'INET_PTON', 'TIMEDIFF', 'THREADS',
# 'FIRSTFILES' 等变量
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
# 包含转换后的 Makefile.inc.cmake 文件，以便使用其中定义的变量
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")

# 若启用了 CURL 调试
if(ENABLE_CURLDEBUG)
  # 必须单独编译此源文件，以避免 memdebug.h 重定义影响它们
  set_source_files_properties("../../lib/curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
endif()

# 添加自定义命令，用于生成 lib1521.c 文件
add_custom_command(
  # 输出文件为 lib1521.c
  OUTPUT "lib1521.c"
  # 调用 mk-lib1521.pl 脚本处理 curl.h 文件内容，生成 lib1521.c 文件
  COMMAND ${PERL_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/mk-lib1521.pl" < "${PROJECT_SOURCE_DIR}/include/curl/curl.h" "lib1521.c"
  # 该命令依赖于 mk-lib1521.pl 脚本和 curl.h 文件
  DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/mk-lib1521.pl"
    "${PROJECT_SOURCE_DIR}/include/curl/curl.h"
  # 确保命令参数按原样传递
  VERBATIM)

# 若启用了 CURL 测试捆绑功能
if(CURL_TEST_BUNDLES)
  # 添加自定义命令，用于生成 libtest_bundle.c 文件
  add_custom_command(
    # 输出文件为 libtest_bundle.c
    OUTPUT "libtest_bundle.c"
    # 调用 mk-bundle.pl 脚本处理当前源目录内容，生成 libtest_bundle.c 文件
    COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/tests/mk-bundle.pl" "${CMAKE_CURRENT_SOURCE_DIR}" > "libtest_bundle.c"
    # 该命令依赖于 mk-bundle.pl 脚本、FIRSTFILES 列表、lib1521.c 文件和 Makefile.inc 文件
    DEPENDS
      "${PROJECT_SOURCE_DIR}/tests/mk-bundle.pl" ${FIRSTFILES} "lib1521.c"
      "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc"
    # 确保命令参数按原样传递
    VERBATIM)

  # 设置测试程序名称为 libtests
  set(LIBTESTPROGS "libtests")
  # 设置 libtests 程序的源文件为 libtest_bundle.c
  set(libtests_SOURCES "libtest_bundle.c")
  # 将 TESTUTIL 和 TSTTRACE 添加到 libtests 程序的源文件列表中
  list(APPEND libtests_SOURCES ${TESTUTIL} ${TSTTRACE})
  # 若选择链接共享库
  if(LIB_SELECTED STREQUAL LIB_SHARED)
    # 这些源文件是 libcurl 静态库的一部分，链接共享库时需要添加到测试程序源文件列表中
    list(APPEND libtests_SOURCES ${WARNLESS} ${MULTIBYTE} ${INET_PTON} ${TIMEDIFF} ${THREADS})
  endif()
endif()

# 遍历 LIBTESTPROGS 列表中的每个测试程序目标
foreach(_target IN LISTS LIBTESTPROGS)
  # 若定义了 ${_target}_SOURCES 变量
  if(DEFINED ${_target}_SOURCES)
    # 设置源文件列表为 ${_target}_SOURCES
    set(_sources ${${_target}_SOURCES})
  else()
    # 否则设置源文件列表为 nodist_${_target}_SOURCES
    set(_sources ${nodist_${_target}_SOURCES})
  endif()

  # 若选择链接静态库
  if(LIB_SELECTED STREQUAL LIB_STATIC)
    # 这些源文件是 libcurl 静态库的一部分，链接静态库时不再重复编译和链接
    list(REMOVE_ITEM _sources ${WARNLESS} ${MULTIBYTE} ${INET_PTON} ${TIMEDIFF} ${THREADS})
  endif()

  # 将目标名称转换为大写
  string(TOUPPER ${_target} _upper_target)
  # 设置目标名称
  set(_target_name "${_target}")
  # 添加可执行文件目标，从默认构建目标中排除
  add_executable(${_target_name} EXCLUDE_FROM_ALL ${_sources})
  # 添加依赖项，确保 testdeps 目标构建完成后再构建当前目标
  add_dependencies(testdeps ${_target_name})
  # 将 LIB_SELECTED 和 CURL_LIBS 链接到当前可执行文件目标
  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_LIBS})
  # 为当前可执行文件目标添加私有包含目录
  target_include_directories(${_target_name} PRIVATE
    "${PROJECT_BINARY_DIR}/lib"            # 用于查找 curl_config.h
    "${PROJECT_SOURCE_DIR}/lib"            # 用于查找 curl_setup.h
    "${PROJECT_SOURCE_DIR}/src"            # 用于查找 tool_binmode.h
    "${PROJECT_SOURCE_DIR}/tests/libtest"  # 用于构建生成的测试文件
  )
  # 为当前可执行文件目标添加编译定义，包含 CURL 调试宏
  set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "${CURL_DEBUG_MACROS}")
  # 若未启用 CURL 测试捆绑功能
  if(NOT CURL_TEST_BUNDLES)
    # 为当前可执行文件目标添加编译定义，定义为大写的目标名称
    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS ${_upper_target})
  endif()
  # 设置当前可执行文件目标的属性
  set_target_properties(${_target_name} PROPERTIES
    OUTPUT_NAME "${_target}"  # 设置输出文件名称
    PROJECT_LABEL "Test libtest ${_target}"  # 设置项目标签
  )
endforeach()
